/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package top.dzygod.modules.pay.service.impl;

import top.dzygod.modules.pay.domain.Record;
import top.dzygod.utils.ValidationUtil;
import top.dzygod.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import top.dzygod.modules.pay.repository.RecordRepository;
import top.dzygod.modules.pay.service.RecordService;
import top.dzygod.modules.pay.service.dto.RecordDto;
import top.dzygod.modules.pay.service.dto.RecordQueryCriteria;
import top.dzygod.modules.pay.service.mapstruct.RecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import top.dzygod.utils.PageUtil;
import top.dzygod.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://dzygod.top
* @description 服务实现
* @author dzygod
* @date 2020-07-28
**/
@Service
@RequiredArgsConstructor
public class RecordServiceImpl implements RecordService {

    private final RecordRepository recordRepository;
    private final RecordMapper recordMapper;

    @Override
    public Map<String,Object> queryAll(RecordQueryCriteria criteria, Pageable pageable){
        Page<Record> page = recordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(recordMapper::toDto));
    }

    @Override
    public List<RecordDto> queryAll(RecordQueryCriteria criteria){
        return recordMapper.toDto(recordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RecordDto findById(Long recordId) {
        Record record = recordRepository.findById(recordId).orElseGet(Record::new);
        ValidationUtil.isNull(record.getId(),"Record","recordId",recordId);
        return recordMapper.toDto(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RecordDto create(Record resources) {
        return recordMapper.toDto(recordRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Record resources) {
        Record record = recordRepository.findById(resources.getId()).orElseGet(Record::new);
        ValidationUtil.isNull( record.getId(),"Record","id",resources.getId());
        record.copy(resources);
        recordRepository.save(record);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long recordId : ids) {
            recordRepository.deleteById(recordId);
        }
    }

    @Override
    public void download(List<RecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RecordDto record : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("学校", record.getSchool().getName());
            map.put("年级", record.getGrade().getName());
            map.put("班级", record.getClazz().getName());
            map.put("学生姓名", record.getName());
            map.put("缴费金额", record.getTotalFee());
            map.put("添加时间", record.getGmtCreate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
